package com.wwz.config;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.rabbit.listener.RabbitListenerContainerFactory;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/*@Configuration*/
public class RabbitMqConfig {

    @Bean
    public Queue singleQueue(){
        Map<String,Object> arguments = new HashMap<>();
      /*  arguments.put("x-dead-letter-exchange", QueueEnum.SIGNLE.getExchangeName());
        arguments.put("x-dead-letter-routing-key", QueueEnum.SIGNLE.getBindKeyName());*/
        return new Queue(QueueEnum.SIGNLE.getQueueName(),true,false,false,arguments);
    }

    /*@Bean
    public RabbitListenerContainerFactory<?> rabbitListenerContainerFactory(ConnectionFactory connectionFactory){
        SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
        factory.setConnectionFactory(connectionFactory);
        factory.setMessageConverter(new Jackson2JsonMessageConverter());
        factory.setAcknowledgeMode(AcknowledgeMode.MANUAL);             //开启手动 ack
        return factory;
    }*/

   private final static Logger log = LoggerFactory.getLogger(RabbitMqConfig.class);
    public enum QueueEnum{
        SIGNLE("single_queue","exchange_1","bindkey");
        QueueEnum(String queueName,String exchangeName,String bindKeyName){
            this.queueName = queueName;
            this.exchangeName = exchangeName;
            this.bindKeyName = bindKeyName;
        }
        private String queueName;
        private String exchangeName;
        private String bindKeyName;

        public String getQueueName() {
            return queueName;
        }

        public void setQueueName(String queueName) {
            this.queueName = queueName;
        }

        public String getExchangeName() {
            return exchangeName;
        }

        public void setExchangeName(String exchangeName) {
            this.exchangeName = exchangeName;
        }

        public String getBindKeyName() {
            return bindKeyName;
        }

        public void setBindKeyName(String bindKeyName) {
            this.bindKeyName = bindKeyName;
        }
    }


    @Bean
    public RabbitTemplate rabbitTemplate(@Autowired ConnectionFactory connectionFactory) {
        RabbitTemplate rabbitTemplate = new RabbitTemplate();
        rabbitTemplate.setConnectionFactory(connectionFactory);
        rabbitTemplate.setChannelTransacted(false);
        rabbitTemplate.setMandatory(true);
        rabbitTemplate.setConfirmCallback(new RabbitTemplate.ConfirmCallback() {
            @Override
            public void confirm(CorrelationData correlationData, boolean ack, String cause) {
                if(ack){
                    log.info("消息发送成功:correlationData({}),ack({}),cause({})", correlationData, ack, cause);

                }else{
                    System.out.print("----");
                }
            }
        });

        rabbitTemplate.setReturnCallback(new RabbitTemplate.ReturnCallback() {
            @Override
            public void returnedMessage(Message message, int replyCode, String replyText, String exchange, String routingKey) {
                message.getMessageProperties().setDeliveryMode(MessageProperties.DEFAULT_DELIVERY_MODE);
                log.info("消息丢失:exchange({}),route({}),replyCode({}),replyText({}),message:{}", exchange, routingKey, replyCode, replyText, message);
            }
        });
        return rabbitTemplate;
    }



 /*   @Bean
    public DirectExchange directExchange(){
        return ExchangeBuilder.directExchange(QueueEnum.SIGNLE.getExchangeName()).durable(true).build();
    }*/

    /**
     * 创建路由
     */
    @Bean
    public CustomExchange createExchange(){
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("x-delayed-type", "direct");
        return new CustomExchange(
                QueueEnum.SIGNLE.getExchangeName(), "x-delayed-message", true, false, map);
    }
    @Bean
    public Binding binding(Queue singleQueue){
        return BindingBuilder.bind(singleQueue).to(createExchange()).with(QueueEnum.SIGNLE.getBindKeyName()).noargs();
    }
}
